package yja.gzw.app.fragmentview

import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener
import yja.gzw.app.R
import yja.gzw.app.adapter.ChengjieAdapter
import yja.gzw.app.base.BaseFragment
import yja.gzw.app.databinding.FragmentChengjieliuBinding
import yja.gzw.app.databinding.FragmentChengjieyiBinding
import yja.gzw.app.model.UserData
import yja.gzw.app.model.bean.OrderDatachengjie
import yja.gzw.app.viewmodel.Mainviewmodel

class ChengjieliuFragment :BaseFragment<Mainviewmodel, FragmentChengjieliuBinding>(R.layout.fragment_chengjieliu){
    private lateinit var fabiada: ChengjieAdapter
    var yeshu=1
    var shuju= mutableListOf<OrderDatachengjie>()
    var zongshuju= mutableListOf<OrderDatachengjie>()


    override fun initView() {

        zongshuju.clear()
        viewmodel.onchengjiewang(UserData.token, mapOf("keyword" to "","status" to "-2","page" to "1","pagesize" to "10"))
        viewmodel.onchengjiewang(UserData.token, mapOf("keyword" to "","status" to "-1","page" to "1","pagesize" to "10"))
        viewmodel.onchengjiewang(UserData.token, mapOf("keyword" to "","status" to "-3","page" to "1","pagesize" to "10"))

        binding.refreshLayout.setRefreshHeader(ClassicsHeader(context));
        binding.refreshLayout.setOnRefreshListener(object : OnRefreshListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                zongshuju.clear()
                yeshu=1
                viewmodel.onchengjiewang(UserData.token, mapOf("keyword" to "","status" to "-1","page" to "1","pagesize" to "10"))
                viewmodel.onchengjiewang(UserData.token, mapOf("keyword" to "","status" to "-2","page" to "1","pagesize" to "10"))
                viewmodel.onchengjiewang(UserData.token, mapOf("keyword" to "","status" to "-3","page" to "1","pagesize" to "10"))

            }


        })

    }

    override fun dataObservese() {

        viewmodel.onchengjie.observe(this, Observer {
            if(it.code==1){
                zongshuju.addAll(it.data.data)

                binding.recyclerView.layoutManager=
                    LinearLayoutManager(context)
                fabiada= ChengjieAdapter(requireContext(),zongshuju)
                binding.recyclerView.adapter= fabiada
                binding.refreshLayout.finishRefresh();
                binding.refreshLayout.finishLoadMore()


            }else{

                Toast.makeText(context,it.msg, Toast.LENGTH_SHORT).show()
            }


        })

    }
}